/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.inmp.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;

import com.sinodata.bsm.inmp.Startup;
import com.sinodata.bsm.inmp.opconf.OpEventDelete;
import com.sinodata.bsm.inmp.task.TaskManager;
import com.sinodata.bsm.inmp.util.SystemContext;
import com.sinodata.bsm.common.constants.SystemStatus;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-8-20 下午3:49:52          tangli         1.0         To create
 * </p>
 *
 * @since  
 * @see     
 */
public class SystemService {

    private static Logger logger = Logger.getLogger(SystemService.class);

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 
     */
    public SystemService() {

    }

    /**
     * 获取probe的状态
     * @return
     */
    public String[] getProbeStatus() {
        int taskSize = TaskManager.getInstance().getFixTaskCount();
        String[] result = { "" + taskSize };
        return result;
    }

    /**
     * 让probe重启
     * @return
     */
    public String stopProbe() {
        Startup.next(SystemStatus.STATUS_RESTART);
        logger.info("接到重启命令  开始重启...");
        return "";
    }

    /**
     * 获取当前时间
     * @return
     */
    public String getCurrenTime() {
        return sdf.format(new Date());
    }

    /**
     * 获取一个probe中的资源总数
     * @return
     */
    public String getResCount() {

        return "";
    }

    /**
     * 查看quartz任务数
     * @return
     */
    public String[] getProbeTask() {
        StringBuffer bufStr = new StringBuffer();
        String[] stry = new String[4];
        stry[0] = (String) SystemContext.getProperty("quatrz.thread");
        stry[1] = TaskManager.getInstance().getFixTaskCount() + "";
        try {
            List<JobExecutionContext> jobs = TaskManager.getInstance().getCurrentlyExecutingJobs();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (int i = 0; i < jobs.size(); i++) {
                JobExecutionContext context = jobs.get(i);
                JobDataMap dataMap = context.getJobDetail().getJobDataMap();
                Long resId = dataMap.getLong("JOB_KEY_RESID");
                Long collTaskId = dataMap.getLong("JOB_KEY_TASKID");
                long begin = dataMap.getLong("begin");
                long curr = new Date().getTime();
                bufStr.append(resId);
                bufStr.append("#").append(collTaskId);
                bufStr.append("#").append(formatter.format(new Date(begin)));
                bufStr.append("#").append((curr - begin) / (1000d));
                bufStr.append(",");
            }
            stry[2] = jobs.size() + "";
            stry[3] = bufStr.toString();
        } catch (Exception e) {
            logger.error(e);
        }
        return stry;
    }

    /**
     * 获取虚拟机信息
     * @return
     */
    public String[] getJvmInfo() {
        String[] stry = new String[6];
        stry[0] = Runtime.getRuntime().maxMemory() / (1024 * 1024) + "";
        stry[1] = Runtime.getRuntime().totalMemory() / (1024 * 1024) + "";
        stry[2] = Runtime.getRuntime().freeMemory() / (1024 * 1024) + "";
        stry[3] = System.getProperty("java.version");
        stry[4] = System.getProperty("java.vendor");
        stry[5] = "NO";
        return stry;
    }

    /**
     * 清除Opmanager中的事件
     * @param objId
     */
    public void clearEvent(String objId) {
        if (objId == null || objId.length() < 1) {
            return;
        }
        OpEventDelete.getInstance().deleteOpEvent(objId);

    }
}
